﻿Imports System.Data.SqlClient
Imports extranet.BE
Imports MySql.Data.MySqlClient

Public Class DAFranquia
  Dim objBD As New BancoDeDados
  Public objQuery As New BEQuery

  Sub New()
  End Sub

  Sub New(ByVal booCondicaoSimilar As Boolean)
    objQuery.Where.CondicaoSimilar = booCondicaoSimilar
  End Sub

  Public Sub Cadastra(ByRef objBEFranquia As BEFranquia)
    Try
      objBD.duplicaAspasSimples(objBEFranquia, GetType(BEFranquia))
      With objBEFranquia
        .IdFranquia = _
        objBD.ExecutaEscalar("INSERT INTO ext_franquia (nome, avatar, pais, cep, uf, cidade, logradouro, " & _
                                                       "telefone, informacoes, email) " & _
                             "VALUES ('" & .Nome & "', '" & _
                                           .Avatar & "', '" & _
                                           .Pais & "', '" & _
                                           .Cep & "', '" & _
                                           .Uf & "', '" & _
                                           .Cidade & "', '" & _
                                           .Logradouro & "', '" & _
                                           .Telefone & "', '" & _
                                           .Informacoes & "', '" & _
                                           .Email & "');")
      End With
    Catch ex As ApplicationException
      Throw New ApplicationException("extranet.DA.DAFranquia.Cadastra(): " & ex.Message)
    End Try
  End Sub

  Public Sub Atualiza(ByVal objBEFranquia As BEFranquia)
    Try
      objBD.duplicaAspasSimples(objBEFranquia, GetType(BEFranquia))
      With objBEFranquia
        objBD.ExecutaNonQuery("UPDATE ext_franquia " & _
                              "SET nome='" & .Nome & "', " & _
                                  "pais='" & .Pais & "', " & _
                                  "uf='" & .Uf & "', " & _
                                  "cidade='" & .Cidade & "', " & _
                                  "cep='" & .Cep & "', " & _
                                  "logradouro='" & .Logradouro & "', " & _
                                  "telefone='" & .Telefone & "', " & _
                                  "email='" & .Email & "', " & _
                                  "avatar='" & .Avatar & "', " & _
                                  "informacoes='" & .Informacoes & "' " & _
                              "WHERE id_franquia=" & .IdFranquia & ";")
      End With
    Catch ex As ApplicationException
      Throw New ApplicationException("extranet.DA.DAFranquia.Atualiza(): " & ex.Message)
    End Try
  End Sub

  Public Sub Exclui(ByVal objBEFranquia As BEFranquia)
    Try
      objBD.ExecutaNonQuery("DELETE FROM ext_franquia WHERE id_franquia='" & objBEFranquia.IdFranquia & "';")
    Catch ex As ApplicationException
      Throw New ApplicationException("extranet.DA.DAFranquia.Exclui(): " & ex.Message)
    End Try
  End Sub

  Public Function Seleciona(ByVal objBEFranquia As BEFranquia) As List(Of BEFranquia)
    Dim objBEFranquias As List(Of BEFranquia) = New List(Of BEFranquia)
    Try
      objBD.duplicaAspasSimples(objBEFranquia, GetType(BEFranquia))
      objQuery._Select = "SELECT * FROM ext_franquia"
      If objBEFranquia IsNot Nothing Then
        If objBEFranquia.IdFranquia > 0 Then objQuery.Where.AdicionarCondicaoWhere("id_franquia", objBEFranquia.IdFranquia, False)
        objQuery.Where.AdicionarCondicaoWhere("nome", objBEFranquia.Nome)
      End If
      objBEFranquias = SelecionaFranquias()
    Catch ex As ApplicationException
      Throw New ApplicationException("extranet.DA.DAFranquia.Seleciona(): " & ex.Message)
    End Try
    Return objBEFranquias
  End Function

  Private Function SelecionaFranquias() As List(Of BEFranquia)
    Dim Franquias As List(Of BEFranquia) = New List(Of BEFranquia)
    Try
      Using objSQLCommand As MySqlCommand = New MySqlCommand(objQuery.Query, BancoDeDados.ConnectionInstance())
        Using objSQLDReader As MySqlDataReader = objSQLCommand.ExecuteReader()
          While objSQLDReader.Read()
            Dim Franquia As New BEFranquia()
            With Franquia
              .IdFranquia = objSQLDReader("id_franquia")
              .Nome = objSQLDReader("nome").ToString()
              .Pais = objSQLDReader("pais").ToString()
              .Uf = objSQLDReader("uf").ToString()
              .Cidade = objSQLDReader("cidade").ToString()
              .Cep = objSQLDReader("cep").ToString()
              .Logradouro = objSQLDReader("logradouro").ToString()
              .Telefone = objSQLDReader("telefone").ToString()
              .Email = objSQLDReader("email").ToString()
              .Avatar = objSQLDReader("avatar").ToString()
              .Informacoes = objSQLDReader("informacoes").ToString()
            End With
            Franquias.Add(Franquia)
          End While
        End Using
      End Using
    Catch ex As ApplicationException
      Throw New ApplicationException("extranet.DA.DAFranquia.SelecionaFranquias(): " & ex.Message)
    End Try
    Return Franquias
  End Function

End Class
